﻿using System;
using System.Dynamic;
using System.IO;
using Hammock.Serialization;
using Newtonsoft.Json;
using Hammock;

namespace WWB.DisqusSharp.Infrastructure.HammockWrappers
{
    public class JsonDotNetSerializer : Utf8Serializer, ISerializer, IDeserializer
    {
        private readonly JsonSerializer _serializer;

        public JsonDotNetSerializer()
            : this(new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include
            })
        {

        }

        public JsonDotNetSerializer(JsonSerializerSettings settings)
        {
            _serializer = new JsonSerializer
            {
                ConstructorHandling = settings.ConstructorHandling,
                ContractResolver = settings.ContractResolver,
                ObjectCreationHandling = settings.ObjectCreationHandling,
                MissingMemberHandling = settings.MissingMemberHandling,
                DefaultValueHandling = settings.DefaultValueHandling,
                NullValueHandling = settings.NullValueHandling
            };

            foreach (var converter in settings.Converters)
            {
                _serializer.Converters.Add(converter);
            }
        }

        #region IDeserializer Members

        public virtual object Deserialize(string content, Type type)
        {
            using (var stringReader = new StringReader(content))
            {
                using (var jsonTextReader = new JsonTextReader(stringReader))
                {
                    return _serializer.Deserialize(jsonTextReader, type);
                }
            }
        }

        public virtual T Deserialize<T>(string content)
        {
            using (var stringReader = new StringReader(content))
            {
                using (var jsonTextReader = new JsonTextReader(stringReader))
                {
                    return _serializer.Deserialize<T>(jsonTextReader);
                }
            }
        }

        #endregion

        #region ISerializer Members

        public virtual string Serialize(object instance, Type type)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    jsonTextWriter.Formatting = Formatting.Indented;
                    jsonTextWriter.QuoteChar = '"';

                    _serializer.Serialize(jsonTextWriter, instance);

                    var result = stringWriter.ToString();
                    return result;
                }
            }
        }

        public virtual string ContentType
        {
            get { return "application/json"; }
        }

        #endregion

        //public object Deserialize(RestResponse response, Type type)
        //{
        //    throw new NotImplementedException();
        //}

        //public T Deserialize<T>(RestResponse<T> response)
        //{
        //    throw new NotImplementedException();
        //}

        //public dynamic DeserializeDynamic<T>(RestResponse<T> response) where T : DynamicObject
        //{
        //    throw new NotImplementedException();
        //}
        public object Deserialize(RestResponseBase response, Type type)
        {
            throw new NotImplementedException();
        }

        public T Deserialize<T>(RestResponseBase response)
        {
            throw new NotImplementedException();
        }

        public dynamic DeserializeDynamic(RestResponseBase response)
        {
            throw new NotImplementedException();
        }
    }
}
